<!-- 商务经理信息管理 -->
<template>
    <div >
        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small" >
            <div slot="state" slot-scope="state">
                <a-badge :status="state | contractStatusTypeFilter" :text="state | contractStatusFilter" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-popconfirm
                        title="是否停用当前客户信息?"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="setState(record,0)"
                        v-if="record.state == 1"
                    >
                        <a-button type="link">停用</a-button>
                    </a-popconfirm>
                    <a-popconfirm
                        title="是否启用当前客户信息?"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="setState(record,1)"
                        v-else
                    >
                        <a-button type="link">启用</a-button>
                    </a-popconfirm>
                    <a-button type="link" @click="update(record)">编辑</a-button>
                </div>
            </div>
        </a-table>

        <!-- 添加商务经理 -->
        <a-modal 
            v-model="visible" 
            :title="isAdd ? '添加商务经理' : '编辑商务经理'"
            @ok="handleOk" 
            @cancel="visible = false">
            <div>
                <a-form-model :model="info" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="姓名" prop="userName">
                        <a-input v-model="info.userName" :disabled="!isAdd" allow-clear placeholder="请输入姓名" />
                    </a-form-model-item>
                    <a-form-model-item label="手机号" prop="phone">
                        <a-input v-model="info.phone" :disabled="!isAdd" allow-clear placeholder="请输入手机号" />
                    </a-form-model-item>
                    <a-form-model-item label="昵称" >
                        <a-input v-model="info.nickName" allow-clear placeholder="请输入昵称" />
                    </a-form-model-item>
                    <a-form-model-item label="微信号" >
                        <a-input v-model="info.wxNum" allow-clear placeholder="请输入微信号" />
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { businessManageList , createBusinessManage , updateBusinessManage , changeBusinessManageState } from '@/api/services/share';
export default {
    data () {
        return {
            columns,
            isload: false,
            dataList: [],
            page: 1,
            size: 10,
            total: 0,
            isAdd: true,
            info: {
                id: '',
                userName: '', // 姓名
                nickName: '', // 昵称
                phone: '', // 手机号
                wxNum: '', // 微信号
            },
            rules: {
                userName: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                ],
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'}
                ],
            },
            visible: false,
            statusList: [
                { id: 0, name: '使用中'},
                { id: 1, name: '已停用'}
            ],
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 获取列表数据
        getData(){
            let _this = this;
            _this.isload = true;
            const params = {
                'pageSize': _this.size,
                'pageIndex': _this.page,
            }
            businessManageList(params)
            .then(res=> {
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加商务经理
        add(){
            this.info.id = ''
            this.isAdd = true
            this.visible = true
        },
        // 编辑
        update(item){
            this.info = JSON.parse(JSON.stringify(item))
            this.isAdd = false;
            this.visible = true;
        },
        handleOk(){
            let _this = this;
            _this.$refs.ruleForm.validate(valid => {
                if(valid){
                    var api = _this.isAdd ? createBusinessManage(_this.info) : updateBusinessManage(_this.info);
                    api.then(res=>{
                        if(res.success){
                            _this.visible = false;
                            _this.getData();
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: _this.isAdd ? '添加成功' : '修改成功',
                                duration: 8
                            })
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 商务经理状态切换
        setState(item,state){
            let _this = this;
            const params = {
                'state': state,
                'id': item.id,
            }
            changeBusinessManageState(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: state == 1 ? '启用成功' : '停用成功',
                        duration: 8
                    })
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        }
    }
}

</script>
<style scoped>

</style>